package com.app.business.web.business;

import com.app.business.model.*;
import com.app.business.service.*;
import com.app.business.utils.CareBuyAreaUtils;
import com.app.system.jpa.PageBean;
import com.app.system.log4j2.LogUtils;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import freemarker.template.Configuration;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 明细查看导出专用
 * Created by DDZ on 2019/6/22.
 *
 * @author ddz
 */
@Controller
@RequestMapping("/business/statistics/excel/*")
@SuppressWarnings("all")
public class ExcelController {

    @Autowired
    private AzProjectService azProjectService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private StorageRoomService storageRoomService;
    @Autowired
    private CarLotService carLotService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private BuyHousePersonService buyHousePersonService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private StatisticsService statisticsService;
    private Logger logger = LogUtils.getBusinessLogger();
    @Autowired
    private Configuration freemarkerConfiguration; // 注入bean，这里是属性注入，推荐构造注入
    @Autowired
    private HandInMoneyService handInMoneyService;
    @Autowired
    private ShopHouseService shopHouseService;



    /**
     * 已选选房号明细查看
     */
    @RequestMapping("selectedContractList")
    @RequiresPermissions("excel:selectedContractList")
    public void selectedContractList() {
    }

    /**
     * 已选选房号明细查看加载数据
     *
     * @return
     */
    @RequestMapping("loadSelectedContractList")
    @ResponseBody
    @RequiresPermissions("excel:loadSelectedContractList")
    public Map loadContractList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<Contract> contractPageBean = this.contractService.getChoosedHousePageBean();
        List<Contract> contractList = contractPageBean.getDatas();
        Map<String, DataDict> signMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> checkMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");
        if (!CollectionUtils.isEmpty(contractList)) {
            Map<String, Object> item = null;
            for (Contract contract : contractList) {
                item = null;
                item = Maps.newHashMap();
                item.put("id", contract.getId());
                item.put("town", StringUtils.dataDictShow(townMap.get(contract.getZsProject().getTown())));
                item.put("zsProjectName", contract.getZsProject().getProjectName());
                item.put("contractCode", StringUtils.getNotNull(contract.getContractCode()));
                item.put("name", StringUtils.getNotNull(contract.getName()));
                item.put("chooseHouseNum", StringUtils.getNotNull(contract.getChooseHouseNum()));
                item.put("limitHouseAreaTotal", BigDecimalUtils.getNotNullStr(contract.getLimitHouseAreaTotal()));
                item.put("financeCheckStatus", IntegerUtils.getNotNull(contract.getFinanceCheckStatus()));
                item.put("financeCheckStatusStr", StringUtils.dataDictShow(checkMap.get(IntegerUtils.getNotNull(contract.getFinanceCheckStatus()).toString())));
                item.put("azSignStatus", IntegerUtils.getNotNull(contract.getAzSignStatus()));
                item.put("azSignStatusStr", StringUtils.dataDictShow(signMap.get(IntegerUtils.getNotNull(contract.getAzSignStatus()).toString())));
                listData.add(item);
            }
        }
        return LayUiUtils.page(contractPageBean.getTotalCount().intValue(), listData);
    }
    /**
     * 已选选房号信息导出
     */
    @RequestMapping("selectedContractListExcel")
    @RequiresPermissions("excel:selectedContractListExcel")
    public void selectedContractListExcel(HttpServletRequest request, HttpServletResponse response) {
        PageBean<Contract> contractPageBean = this.contractService.getChoosedHouseExcelPageBean();
        List<Contract> contractList = contractPageBean.getDatas();
        Map<String, DataDict> chooseHouseStatusMap = this.dataDictService.getMapByParentNameForValue("房源签认状态");
        Map<String, DataDict> financeCheckStatusMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> azSignStatusMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> financeSettleStatusMap = this.dataDictService.getMapByParentNameForValue("财务结清状态");
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        Map<String, DataDict> takeHouseStatusMap = this.dataDictService.getMapByParentNameForValue("交房状态");

        Map<String, List<House>> houseMap = this.houseService.getHouseByContractIdMap();
        Map<String, List<StorageRoom>> storageRoomMap = this.storageRoomService.getStorageRoomByContractIdMap();
        Map<String, List<CarLot>> carLotMap = this.carLotService.getHouseByContractIdMap();
        Map<String, List<ShopHouse>> shopHouseMap = this.shopHouseService.getShopHouseByContractIdMap();


        Map<String, List<Map>> buyHousePersonHouseMap = this.buyHousePersonService.getBuyHousePersonByHouse();
        Map<String, List<Map>> buyHousePersonCarLotMap = this.buyHousePersonService.getBuyHousePersonByCarLot();
        Map<String, List<Map>> buyHousePersonShopHouseMap = this.buyHousePersonService.getBuyHousePersonByShopHouse();
        Map<String,Map> handMoneyDateByConId = this.handInMoneyService.getMoneyAndDateByConId();
        List<String> careBuyAreaNameList = CareBuyAreaUtils.getCareBuyAreaNameListForExcel();

        try {
            // 封装导出数据
            Map data = Maps.newHashMap();
            data.put("careBuyAreaNameList", careBuyAreaNameList);
            data.put("contractList", contractList);
            data.put("houseMap", houseMap);
            data.put("storageRoomMap", storageRoomMap);
            data.put("carLotMap", carLotMap);
            data.put("shopHouseMap", shopHouseMap);

            data.put("chooseHouseStatusMap", chooseHouseStatusMap);
            data.put("financeCheckStatusMap", financeCheckStatusMap);
            data.put("azSignStatusMap", azSignStatusMap);
            data.put("financeSettleStatusMap", financeSettleStatusMap);

            data.put("buyHousePersonHouseMap", buyHousePersonHouseMap);
            data.put("buyHousePersonCarLotMap", buyHousePersonCarLotMap);
            data.put("buyHousePersonShopHouseMap", buyHousePersonShopHouseMap);
            data.put("houseHoldMap", houseHoldMap);
            data.put("carLotTypeMap", carLotTypeMap);
            data.put("takeHouseStatusMap",takeHouseStatusMap);
            data.put("handMoneyDateByConId",handMoneyDateByConId);
            data.put("exportPerson", SecurityUtils.getSubject().getPrincipal());   //导出人姓名
            data.put("exportTime", DateUtils.dateToStringFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));   //导出时间
//            data.put("decisions", exportDecisionList); //第二个worksheet的数据对象
            request.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(("已选选房号明细导出.xls").getBytes(), "iso-8859-1"));
            freemarkerConfiguration.getTemplate("/business/statistics/excel/selectedContractListExcel.ftl").process(data, response.getWriter());
        } catch (Exception e) {
            logger.error("文件下载异常", e);
            e.printStackTrace();
        }
    }
    /**
     * 已选房源明细查看
     */
    @RequestMapping("selectedHouseList")
    @RequiresPermissions("excel:selectedHouseList")
    public void selectedHouseList() {
    }

    /**
     * 已选房源明细查看加载数据
     *
     * @return
     */
    @RequestMapping("loadSelectedHouseList")
    @ResponseBody
    @RequiresPermissions("excel:loadSelectedHouseList")
    public Map loadSelectedHouseList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<House> housePageBean = this.houseService.getSelectedHousePageBean();
        Map<String, DataDict> signMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");

        List<House> houseList = housePageBean.getDatas();
        if (CollectionUtils.isNotEmpty(houseList)) {
            Map<String, Object> item = null;
            for (House house : houseList) {
                item = null;
                item = Maps.newHashMap();
                item.put("houseId", house.getId());
                item.put("contractId", house.getContract().getId());
                item.put("zsProjectName", StringUtils.getNotNull(house.getContract().getZsProject().getProjectName()));
                item.put("name", StringUtils.getNotNull(house.getContract().getName()));
                item.put("contractCode", StringUtils.getNotNull(house.getContract().getContractCode()));
                item.put("azSignStatus", IntegerUtils.getNotNull(house.getContract().getAzSignStatus()));
                item.put("azSignStatusStr", StringUtils.dataDictShow(signMap.get(IntegerUtils.getNotNull(house.getContract().getAzSignStatus()).toString())));
                item.put("azProjectName", StringUtils.getNotNull(house.getAzProject().getProjectName()));
                item.put("houseNum", StringUtils.getNotNull(house.getHouseNumStr()));
                item.put("houseHold", StringUtils.getNotNull(house.getHouseHold()));
                item.put("houseHoldStr", StringUtils.dataDictShow(houseHoldMap.get(StringUtils.getNotNull(house.getHouseHold()))));
                item.put("chooseHouseDate", DateUtils.dateToStringDefault(house.getChooseHouseDate()));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(house.getBuildArea()));
                listData.add(item);
            }
        }
        return LayUiUtils.page(housePageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 已选房源信息导出
     */
    @RequestMapping("selectedHouseListExcel")
    @RequiresPermissions("excel:selectedHouseListExcel")
    public void selectedHouseListExcel(HttpServletRequest request, HttpServletResponse response) {
        PageBean<House> housePageBean = this.houseService.getExcelSelectedHousePageBean();
        List<House> houseList = housePageBean.getDatas();
        Map<String, DataDict> azSignStatusMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> financeCheckStatusMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, DataDict> takeHouseStatusMap = this.dataDictService.getMapByParentNameForValue("交房状态");
        Map<String, DataDict> financeSettleStatusMap = this.dataDictService.getMapByParentNameForValue("财务结清状态");
        Map<String, DataDict> deedShStatusMap = this.dataDictService.getMapByParentNameForValue("税费审核情况");
        Map<String, DataDict> bzStatusMap = this.dataDictService.getMapByParentNameForValue("办证情况");
        Map<String, DataDict> zxStatusMap = this.dataDictService.getMapByParentNameForValue("装修情况");
        Map<String, DataDict> rzStatusMap = this.dataDictService.getMapByParentNameForValue("入住情况");
        Map<String, List<Map>> buyHousePersonMap = this.buyHousePersonService.getBuyHousePersonByHouse();
        try {
            // 封装导出数据
            Map data = Maps.newHashMap();
            data.put("houseList", houseList);
            data.put("azSignStatusMap", azSignStatusMap);
            data.put("financeCheckStatusMap", financeCheckStatusMap);
            data.put("buyHousePersonMap", buyHousePersonMap);
            data.put("houseHoldMap", houseHoldMap);
            data.put("takeHouseStatusMap", takeHouseStatusMap);
            data.put("bzStatusMap", bzStatusMap);
            data.put("deedShStatusMap", deedShStatusMap);
            data.put("financeSettleStatusMap", financeSettleStatusMap);
            data.put("zxStatusMap", zxStatusMap);
            data.put("rzStatusMap", rzStatusMap);
            data.put("exportPerson", SecurityUtils.getSubject().getPrincipal());   //导出人姓名
            data.put("exportTime", DateUtils.dateToStringFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));   //导出时间
//            data.put("decisions", exportDecisionList); //第二个worksheet的数据对象
            request.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(("已选房源明细导出.xls").getBytes(), "iso-8859-1"));
            freemarkerConfiguration.getTemplate("/business/statistics/excel/selectedHouseListExcel.ftl").process(data, response.getWriter());
        } catch (Exception e) {
            logger.error("文件下载异常", e);
            e.printStackTrace();
        }
    }


    /**
     * 已选储物间明细查看
     */
    @RequestMapping("selectedStorageRoomList")
    @RequiresPermissions("excel:selectedStorageRoomList")
    public void selectedStorageRoomList() {
    }

    /**
     * 已选储物间明细查看加载数据
     *
     * @return
     */
    @RequestMapping("loadSelectedStorageRoomList")
    @ResponseBody
    @RequiresPermissions("excel:loadSelectedStorageRoomList")
    public Map loadSelectedStorageRoomList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<StorageRoom> storageRoomPageBean = this.storageRoomService.getSelectedStorageRoomPageBean();
        Map<String, DataDict> signMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        List<StorageRoom> storageRoomList = storageRoomPageBean.getDatas();
        if (CollectionUtils.isNotEmpty(storageRoomList)) {
            Map<String, Object> item = null;
            for (StorageRoom storageRoom : storageRoomList) {
                item = null;
                item = Maps.newHashMap();
                item.put("storageRoomId", storageRoom.getId());
                item.put("contractId", storageRoom.getContract().getId());
                item.put("zsProjectName", StringUtils.getNotNull(storageRoom.getContract().getZsProject().getProjectName()));
                item.put("name", StringUtils.getNotNull(storageRoom.getContract().getName()));
                item.put("contractCode", StringUtils.getNotNull(storageRoom.getContract().getContractCode()));
                item.put("azSignStatus", IntegerUtils.getNotNull(storageRoom.getContract().getAzSignStatus()));
                item.put("azSignStatusStr", StringUtils.dataDictShow(signMap.get(IntegerUtils.getNotNull(storageRoom.getContract().getAzSignStatus()).toString())));
                item.put("azProjectName", StringUtils.getNotNull(storageRoom.getAzProject().getProjectName()));
                item.put("houseNum", StringUtils.getNotNull(storageRoom.getHouseNumStr()));
                item.put("chooseHouseDate", DateUtils.dateToStringDefault(storageRoom.getChooseHouseDate()));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(storageRoom.getBuildArea()));
                listData.add(item);
            }
        }
        return LayUiUtils.page(storageRoomPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 已选储物间信息导出
     */
    @RequestMapping("selectedStorageRoomListExcel")
    @RequiresPermissions("excel:selectedStorageRoomListExcel")
    public void selectedStorageRoomListExcel(HttpServletRequest request, HttpServletResponse response) {
        PageBean<StorageRoom> storageRoomPageBean = this.storageRoomService.getExcelSelectedStorageRoomPageBean();
        List<StorageRoom> storageRoomList = storageRoomPageBean.getDatas();
        Map<String, DataDict> azSignStatusMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> financeCheckStatusMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> financeSettleStatusMap = this.dataDictService.getMapByParentNameForValue("财务结清状态");
        Map<String, DataDict> takeHouseStatusMap = this.dataDictService.getMapByParentNameForValue("交房状态");
        Map<String, DataDict> deedShStatusMap = this.dataDictService.getMapByParentNameForValue("税费审核情况");
        Map<String, DataDict> bzStatusMap = this.dataDictService.getMapByParentNameForValue("办证情况");
        Map<String, DataDict> zxStatusMap = this.dataDictService.getMapByParentNameForValue("装修情况");
        Map<String, DataDict> rzStatusMap = this.dataDictService.getMapByParentNameForValue("入住情况");
        Map<String, List<Map>> buyHousePersonMap = this.buyHousePersonService.getBuyHousePersonByStorageRoom();
        Map<String, List<Map>> buyHousePersonHouseMap = this.buyHousePersonService.getBuyHousePersonByHouse();

        try {
            // 封装导出数据
            Map data = Maps.newHashMap();
            data.put("storageRoomList", storageRoomList);
            data.put("azSignStatusMap", azSignStatusMap);
            data.put("financeCheckStatusMap", financeCheckStatusMap);
            data.put("buyHousePersonMap", buyHousePersonMap);
            data.put("buyHousePersonHouseMap", buyHousePersonHouseMap);
            data.put("financeSettleStatusMap", financeSettleStatusMap);
            data.put("takeHouseStatusMap", takeHouseStatusMap);
            data.put("bzStatusMap", bzStatusMap);
            data.put("deedShStatusMap", deedShStatusMap);
            data.put("zxStatusMap", zxStatusMap);
            data.put("rzStatusMap", rzStatusMap);
            data.put("exportPerson", SecurityUtils.getSubject().getPrincipal());   //导出人姓名
            data.put("exportTime", DateUtils.dateToStringFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));   //导出时间
//            data.put("decisions", exportDecisionList); //第二个worksheet的数据对象
            request.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(("已选储物间明细导出.xls").getBytes(), "iso-8859-1"));
            freemarkerConfiguration.getTemplate("/business/statistics/excel/selectedStorageRoomListExcel.ftl").process(data, response.getWriter());
        } catch (Exception e) {
            logger.error("文件下载异常", e);
            e.printStackTrace();
        }
    }

    /**
     * 已选车位明细查看
     */
    @RequestMapping("selectedCarLotList")
    @RequiresPermissions("excel:selectedCarLotList")
    public void selectedCarLotList() {
    }

    /**
     * 已选车位明细查看加载数据
     *
     * @return
     */
    @RequestMapping("loadSelectedCarLotList")
    @ResponseBody
    @RequiresPermissions("excel:loadSelectedCarLotList")
    public Map loadSelectedCarLotList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<CarLot> carLotPageBean = this.carLotService.getSelectedCarLotPageBean();
        Map<String, DataDict> signMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        List<CarLot> carLotList = carLotPageBean.getDatas();
        if (CollectionUtils.isNotEmpty(carLotList)) {
            Map<String, Object> item = null;
            for (CarLot carLot : carLotList) {
                item = null;
                item = Maps.newHashMap();
                item.put("carLotId", carLot.getId());
                item.put("contractId", carLot.getContract().getId());
                item.put("zsProjectName", StringUtils.getNotNull(carLot.getContract().getZsProject().getProjectName()));
                item.put("name", StringUtils.getNotNull(carLot.getContract().getName()));
                item.put("contractCode", StringUtils.getNotNull(carLot.getContract().getContractCode()));
                item.put("azSignStatus", IntegerUtils.getNotNull(carLot.getContract().getAzSignStatus()));
                item.put("azSignStatusStr", StringUtils.dataDictShow(signMap.get(IntegerUtils.getNotNull(carLot.getContract().getAzSignStatus()).toString())));
                item.put("azProjectName", StringUtils.getNotNull(carLot.getAzProject().getProjectName()));
                item.put("houseNum", StringUtils.getNotNull(carLot.getHouseNumStr()));
                item.put("carLotType", IntegerUtils.getNotNull(carLot.getCarLotType()));
                item.put("carLotTypeStr", StringUtils.dataDictShow(carLotTypeMap.get(IntegerUtils.getNotNull(carLot.getCarLotType()).toString())));
                item.put("buildArea", BigDecimalUtils.getNotNull(carLot.getBuildArea()));
                item.put("chooseHouseDate", DateUtils.dateToStringDefault(carLot.getChooseHouseDate()));
                listData.add(item);
            }
        }
        return LayUiUtils.page(carLotPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 已选车位信息导出
     */
    @RequestMapping("selectedCarLotListExcel")
    @RequiresPermissions("excel:selectedCarLotListExcel")
    public void selectedCarLotListExcel(HttpServletRequest request, HttpServletResponse response) {
        PageBean<CarLot> carLotPageBean = this.carLotService.getExcelSelectedCarLotPageBean();
        List<CarLot> carLotList = carLotPageBean.getDatas();
        Map<String, DataDict> azSignStatusMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> financeCheckStatusMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        Map<String, DataDict> takeHouseStatusMap = this.dataDictService.getMapByParentNameForValue("交房状态");
        Map<String, DataDict> financeSettleStatusMap = this.dataDictService.getMapByParentNameForValue("财务结清状态");
        Map<String, DataDict> deedShStatusMap = this.dataDictService.getMapByParentNameForValue("税费审核情况");

        Map<String, DataDict> bzStatusMap = this.dataDictService.getMapByParentNameForValue("办证情况");
        Map<String, DataDict> zxStatusMap = this.dataDictService.getMapByParentNameForValue("装修情况");
        Map<String, DataDict> rzStatusMap = this.dataDictService.getMapByParentNameForValue("入住情况");
        Map<String, List<Map>> buyHousePersonMap = this.buyHousePersonService.getBuyHousePersonByCarLot();


        try {
            // 封装导出数据
            Map data = Maps.newHashMap();
            data.put("carLotList", carLotList);
            data.put("azSignStatusMap", azSignStatusMap);
            data.put("financeSettleStatusMap", financeSettleStatusMap);
            data.put("financeCheckStatusMap", financeCheckStatusMap);
            data.put("buyHousePersonMap", buyHousePersonMap);
            data.put("carLotTypeMap", carLotTypeMap);
            data.put("takeHouseStatusMap", takeHouseStatusMap);
            data.put("bzStatusMap", bzStatusMap);
            data.put("deedShStatusMap", deedShStatusMap);
            data.put("zxStatusMap", zxStatusMap);
            data.put("rzStatusMap", rzStatusMap);
            data.put("exportPerson", SecurityUtils.getSubject().getPrincipal());   //导出人姓名
            data.put("exportTime", DateUtils.dateToStringFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));   //导出时间
//            data.put("decisions", exportDecisionList); //第二个worksheet的数据对象
            request.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(("已选车位明细导出.xls").getBytes(), "iso-8859-1"));
            freemarkerConfiguration.getTemplate("/business/statistics/excel/selectedCarLotListExcel.ftl").process(data, response.getWriter());
        } catch (Exception e) {
            logger.error("文件下载异常", e);
            e.printStackTrace();
        }
    }


    /**
     * 已选选房号情况统计(时间筛选)
     */
    @RequestMapping("selectedContractListByDate")
    @RequiresPermissions("excel:selectedContractListByDate")
    public void selectedContractListByDate() {
    }

    /**
     * 已选选房号情况统计加载数据(时间筛选)
     *
     * @return
     */
    @RequestMapping("loadSelectedContractListByDate")
    @ResponseBody
    @RequiresPermissions("excel:loadSelectedContractListByDate")
    public Map loadSelectedContractListByDate(HttpServletRequest request) {
        String azProjectName = ParamUtils.getString(request, "azProjectName", "");
        String startDate = ParamUtils.getString(request, "startDate", "");
        String stopDate = ParamUtils.getString(request, "stopDate", "");
        List<Map> listData = Lists.newArrayList();
        PageBean<Contract> contractPageBean = this.contractService.getChoosedHouseByAzProjectNamePageBean(azProjectName,startDate,stopDate);
        List<Contract> contractList = contractPageBean.getDatas();
        Map<String, DataDict> signMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> checkMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");
        if (!CollectionUtils.isEmpty(contractList)) {
            Map<String, Object> item = null;
            for (Contract contract : contractList) {
                item = null;
                item = Maps.newHashMap();
                item.put("id", contract.getId());
                item.put("town", StringUtils.dataDictShow(townMap.get(contract.getZsProject().getTown())));
                item.put("zsProjectName", contract.getZsProject().getProjectName());
                item.put("contractCode", StringUtils.getNotNull(contract.getContractCode()));
                item.put("name", StringUtils.getNotNull(contract.getName()));
                item.put("chooseHouseNum", StringUtils.getNotNull(contract.getChooseHouseNum()));
                item.put("limitHouseAreaTotal", BigDecimalUtils.getNotNullStr(contract.getLimitHouseAreaTotal()));
                item.put("financeCheckStatus", IntegerUtils.getNotNull(contract.getFinanceCheckStatus()));
                item.put("financeCheckStatusStr", StringUtils.dataDictShow(checkMap.get(IntegerUtils.getNotNull(contract.getFinanceCheckStatus()).toString())));
                item.put("azSignStatus", IntegerUtils.getNotNull(contract.getAzSignStatus()));
                item.put("azSignStatusStr", StringUtils.dataDictShow(signMap.get(IntegerUtils.getNotNull(contract.getAzSignStatus()).toString())));
                listData.add(item);
            }
        }
        return LayUiUtils.page(contractPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 已选选房号信息导出
     */
    @RequestMapping("selectedContractListByDateExcel")
    @RequiresPermissions("excel:selectedContractListByDateExcel")
    public void selectedContractListByDateExcel(HttpServletRequest request, HttpServletResponse response) {
        PageBean<Contract> contractPageBean = this.contractService.getChoosedHouseExcelPageBean();
        List<Contract> contractList = contractPageBean.getDatas();
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");
        List<String> careBuyAreaNameList = CareBuyAreaUtils.getCareBuyAreaNameListForExcel();

         Map<String,Map> contractIdAndHouseHoldNumMap = this.statisticsService.getHouseStatisticsByContractIdAndHouseHold();

        try {
            // 封装导出数据
            Map data = Maps.newHashMap();

            data.put("careBuyAreaNameList", careBuyAreaNameList);
            data.put("contractList", contractList);
            data.put("townMap", townMap);
            data.put("contractIdAndHouseHoldNumMap", contractIdAndHouseHoldNumMap);
            data.put("exportPerson", SecurityUtils.getSubject().getPrincipal());   //导出人姓名
            data.put("exportTime", DateUtils.dateToStringFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));   //导出时间
//            data.put("decisions", exportDecisionList); //第二个worksheet的数据对象
            request.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(("已回迁明细导出.xls").getBytes(), "iso-8859-1"));
            freemarkerConfiguration.getTemplate("/business/statistics/excel/selectedContractListByDateExcel.ftl").process(data, response.getWriter());
        } catch (Exception e) {
            logger.error("文件下载异常", e);
            e.printStackTrace();
        }
    }

    /**
     * 财务明细查看
     */
    @RequestMapping("financeExportList")
    @RequiresPermissions("excel:financeExportList")
    public void financeExportList() {
    }

    /**
     * 财务明细查看列表加载数据
     *
     * @return
     */
    @RequestMapping("loadFinanceExportList")
    @ResponseBody
    @RequiresPermissions("excel:loadFinanceExportList")
    public Map loadFinanceExportList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<Contract> contractPageBean = this.contractService.getFinanceCheckedPageBean();
        Map<String, DataDict> signMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> checkMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> settleMap = this.dataDictService.getMapByParentNameForValue("财务结清状态");
        Map<String, DataDict> jyMoneyFfStatusMap = this.dataDictService.getMapByParentNameForValue("档案发放状态");

        List<Contract> contractList = contractPageBean.getDatas();
        if (CollectionUtils.isNotEmpty(contractList)) {
            Map<String, Object> item = null;
            BigDecimal finalMoney = null;
            BigDecimal handInMoney = null;
            BigDecimal nowMoney = null;
            for (Contract contract : contractList) {
                item = null;
                finalMoney = null;
                handInMoney = null;
                nowMoney = null;
                item = Maps.newHashMap();
                finalMoney = BigDecimalUtils.getNotNull(contract.getFinalMoney());
                handInMoney = BigDecimalUtils.getNotNull(contract.getHandInMoney());
                if (contract.getHouseOneMoney().compareTo(BigDecimal.ZERO) > 0) {
                    nowMoney = BigDecimalUtils.getSum(contract.getWxMoneyTotal(),contract.getHandInMoney());//公维金肯定是负数
                } else {
                    nowMoney = BigDecimalUtils.getSum(contract.getWxMoneyTotal(),contract.getHouseOneMoney(),contract.getHandInMoney());
                }
                nowMoney = nowMoney.abs();//取绝对值
                item.put("contractId", contract.getId());
                item.put("zsProjectName", StringUtils.getNotNull(contract.getZsProject().getProjectName()));
                item.put("contractCode", StringUtils.getNotNull(contract.getContractCode()));
                item.put("name", StringUtils.getNotNull(contract.getName()));
                item.put("chooseHouseNum", StringUtils.getNotNull(contract.getChooseHouseNum()));
                item.put("houseOneMoney",BigDecimalUtils.getNotNullStr(contract.getHouseOneMoney()));
                item.put("wxMoneyTotal",BigDecimalUtils.getNotNullStr(contract.getWxMoneyTotal()));
                item.put("handInMoney", BigDecimalUtils.getNotNullStr(handInMoney));//已缴款，
                item.put("nowMoney", BigDecimalUtils.getNotNullStr(nowMoney));//剩余应缴款，
                item.put("financeSettleStatus", IntegerUtils.getNotNull(contract.getFinanceSettleStatus()));
                item.put("financeSettleStatusStr", StringUtils.dataDictShow(settleMap.get(IntegerUtils.getNotNull(contract.getFinanceSettleStatus()).toString())));
                item.put("jyMoneyFfStatusStr", StringUtils.dataDictShow(jyMoneyFfStatusMap.get(String.valueOf(contract.getJyMoneyFfStatus()))));
                listData.add(item);
            }
        }
        return LayUiUtils.page(contractPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 财务明细查看信息导出
     */
    @RequestMapping("financeExportListExcel")
    @RequiresPermissions("excel:financeExportListExcel")
    public void financeExportListExcel(HttpServletRequest request, HttpServletResponse response) {
        PageBean<Contract> contractPageBean = this.contractService.getExcelContractPageBean();
        List<Contract> contractList = contractPageBean.getDatas();
        Map<String, DataDict> financeSettleStatusMap = this.dataDictService.getMapByParentNameForValue("财务结清状态");
        Map<String, DataDict> jyMoneyFfStatusMap = this.dataDictService.getMapByParentNameForValue("档案发放状态");
        try {
            // 封装导出数据
            Map data = Maps.newHashMap();
            data.put("contractList", contractList);
            data.put("bigUtils", new BigDecimalUtils());
            data.put("financeSettleStatusMap", financeSettleStatusMap);
            data.put("jyMoneyFfStatusMap", jyMoneyFfStatusMap);
            data.put("exportPerson", SecurityUtils.getSubject().getPrincipal());   //导出人姓名
            data.put("exportTime", DateUtils.dateToStringFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));   //导出时间
//            data.put("decisions", exportDecisionList); //第二个worksheet的数据对象
            request.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(("财务结算明细导出.xls").getBytes(), "iso-8859-1"));
            freemarkerConfiguration.getTemplate("/business/statistics/excel/financeExportListExcel.ftl").process(data, response.getWriter());
        } catch (Exception e) {
            logger.error("文件下载异常", e);
            e.printStackTrace();
        }
    }


}
